package com.example.peibei.ui.fragment;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.dingtao.common.bean.MessageCount;
import com.dingtao.common.bean.User;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.DateUtils;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.bean.SpUserInfoCall;
import com.example.peibei.service.presenter.MessageCountPresenter;
import com.example.peibei.service.presenter.UserInfoPresenter;
import com.example.peibei.ui.activity.AuthCenterActivity;
import com.example.peibei.ui.activity.EditInfoActivity;
import com.example.peibei.ui.activity.FeedbackActivity;
import com.example.peibei.ui.activity.ISendBillActivity;
import com.example.peibei.ui.activity.IdentityCheckedActivity;
import com.example.peibei.ui.activity.LoginActivity;
import com.example.peibei.ui.activity.MainActivity;
import com.example.peibei.ui.activity.MemberActivity;
import com.example.peibei.ui.activity.MyCollectBillActivity;
import com.example.peibei.ui.activity.MyDynamicActivity;
import com.example.peibei.ui.activity.MyFansListActivity;
import com.example.peibei.ui.activity.MyFollowListActivity;
import com.example.peibei.ui.activity.MyGetOrderActivity;
import com.example.peibei.ui.activity.MySendBillActivity;
import com.example.peibei.ui.activity.MySendOrderActivity;
import com.example.peibei.ui.activity.MyVisitHistoryActivity;
import com.example.peibei.ui.activity.MyWaitOrderActivity;
import com.example.peibei.ui.activity.MyWelletActivity;
import com.example.peibei.ui.activity.OrderCancelActivity;
import com.example.peibei.ui.activity.OrderFinishActivity;
import com.example.peibei.ui.activity.OrderPlayingActivity;
import com.example.peibei.ui.activity.OrderWaitActivity;
import com.example.peibei.ui.activity.OrderWaitRecommendActivity;
import com.example.peibei.ui.activity.PersonInfoActivityTwo;
import com.example.peibei.ui.activity.PersonalInfoActivity;
import com.example.peibei.ui.activity.RealNameActivity;
import com.example.peibei.ui.activity.SettingActivity;
import com.example.peibei.ui.activity.TuiguangActivity;
import com.example.peibei.ui.activity.VisitMeHistoryActivity;
import com.example.peibei.ui.utils.LevelImage;
import com.example.peibei.ui.weight.CenterDialog;
import com.example.peibei.ui.weight.CountDownTimer;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.jaeger.library.StatusBarUtil;
import com.tencent.qcloud.tuicore.util.ToastUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class MeFragment extends WDFragment {
    @BindView(R.id.tv_go_login)
    TextView goLogin;
    @BindView(R.id.tv_nickname)
    TextView nickname;
    @BindView(R.id.tv_id)
    TextView id;
    @BindView(R.id.iv_right)
    ImageView iv_right;
    @BindView(R.id.iv_level)
    ImageView level;
    @BindView(R.id.tv_edit)
    TextView edit;
    @BindView(R.id.tv_count_one)
    TextView tv_count_one;
    @BindView(R.id.tv_count_two)
    TextView tv_count_two;
    @BindView(R.id.tv_shoucang)
    TextView tv_shoucang;
    @BindView(R.id.tv_visit_history)
    TextView tv_visit_history;
    @BindView(R.id.tv_see_me)
    TextView tv_see_me;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_vip_end_time)
    TextView tv_vip_end_time;
    @BindView(R.id.pay_vip)
    LinearLayout pay_vip;
    @BindView(R.id.vip_time)
    LinearLayout vip_time;
    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.iv_vip)
    ImageView iv_vip;
    @BindView(R.id.status_bar)
    View fake_status_bar;

    private UserInfoPresenter userInfoPresenter;
    private MessageCountPresenter messageCountPresenter;
    private SPUtils spUtils;
    private String token;

    @Override
    public String getPageName() {
        return "我的Fragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_me;
    }


    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        userInfoPresenter = new UserInfoPresenter(new UserInfoCall());
        messageCountPresenter = new MessageCountPresenter(new MessageCountCall());
    }

    @Override
    public void onResume() {
        super.onResume();
        token = spUtils.getString(SpConstant.USER_TOKEN);
        User loginUser = SpConstant.getLoginUser();
        if (loginUser.getId() == 0) {
            setNoLogin();
        } else {
            userInfoPresenter.reqeust("Bearer " + token);
            messageCountPresenter.reqeust("Bearer " + token);
        }
    }

    private void setNoLogin(){
        goLogin.setVisibility(View.VISIBLE);
        pay_vip.setVisibility(View.VISIBLE);
        nickname.setVisibility(View.GONE);
        id.setVisibility(View.GONE);
        iv_right.setVisibility(View.GONE);
        edit.setVisibility(View.GONE);
        level.setVisibility(View.GONE);
        iv_vip.setVisibility(View.GONE);
        tv_vip_end_time.setVisibility(View.GONE);
        iv_head.setBackgroundResource(R.mipmap.user_head);
        Glide.with(getActivity()).load(R.mipmap.user_head).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(iv_head);
    }

    private void setLogin(int serviceLevel,boolean isVip,String vipTime){
        goLogin.setVisibility(View.GONE);
        nickname.setVisibility(View.VISIBLE);
        id.setVisibility(View.VISIBLE);
        iv_right.setVisibility(View.VISIBLE);
        edit.setVisibility(View.VISIBLE);
        LevelImage.setLevelImage(serviceLevel, level);
        if (isVip) {
            pay_vip.setVisibility(View.GONE);
            vip_time.setVisibility(View.VISIBLE);
            tv_vip_end_time.setVisibility(View.VISIBLE);
            iv_vip.setVisibility(View.VISIBLE);
            try {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd到期");
                Date date = DateUtils.dateParseHour(vipTime,"");
                tv_vip_end_time.setText(format.format(date));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            pay_vip.setVisibility(View.VISIBLE);
            vip_time.setVisibility(View.GONE);
            tv_vip_end_time.setVisibility(View.GONE);
            iv_vip.setVisibility(View.GONE);
        }
    }


    @OnClick(R.id.click)
    public void click() {

    }

    @OnClick(R.id.tv_go_login)
    public void goLogin() {
        intent(LoginActivity.class);
    }

//    @OnClick(R.id.rl_wallet)
//    public void myMoney() {
//        if (TextUtils.isEmpty(token)) {
//            intent(LoginActivity.class);
//            return;
//        }
//        intent(MyWelletActivity.class);
//    }

    @OnClick(R.id.ll_my_money)
    public void myMoney2() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        intent(MyWelletActivity.class);
    }

    @OnClick(R.id.rl_tuiguang)
    public void tuiguang() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        intent(TuiguangActivity.class);
    }

    @OnClick(R.id.rl_feedback)
    public void feedback() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        intent(FeedbackActivity.class);
    }

    @OnClick(R.id.ll_send_order)
    public void sendOrder() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        intent(MySendBillActivity.class);
    }

    @OnClick(R.id.iv_huodong)
    public void tishi() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        intent(MemberActivity.class);
    }

    @OnClick(R.id.ll_order_wait)
    public void getOrder() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
//        intent(MyGetOrderActivity.class);
        intent(OrderWaitActivity.class);
    }

    @OnClick(R.id.ll_order_playing)
    public void orderPlaying() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
//        intent(MyWaitOrderActivity.class);
        intent(OrderPlayingActivity.class);
    }

    @OnClick(R.id.ll_order_finish)
    public void orderFinish() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        intent(OrderFinishActivity.class);
    }

    @OnClick(R.id.ll_my_history)
    public void orderDaipingjia() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        intent(MyVisitHistoryActivity.class);
    }

    @OnClick(R.id.ll_see_me)
    public void seeMe() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        boolean isVip = spUtils.getBoolean(SpConstant.VIP, false);
        if (isVip) {
            intent(VisitMeHistoryActivity.class);
        } else {
            intent(MemberActivity.class);
        }
    }

    //我的评价
//    @OnClick(R.id.ll_pingjia)
//    public void myVisvitHistory() {
//        if (TextUtils.isEmpty(token)) {
//            intent(LoginActivity.class);
//            return;
//        }
//        intent(OrderWaitRecommendActivity.class);
//    }

    @OnClick(R.id.ll_or_cancel)
    public void myOrderCancel() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        intent(OrderCancelActivity.class);
    }

    @OnClick(R.id.rl_setting)
    public void setting() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        intent(SettingActivity.class);
    }

    @OnClick(R.id.rl_identity)
    public void identityCheck() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
//        if (identityBool) {
//            UIUtils.showToastSafe("您已经完成认证");
//            return;
//        }
        intent(AuthCenterActivity.class);
    }

    @OnClick(R.id.tv_edit)
    public void goEdit() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }

        User loginUser = SpConstant.getLoginUser();
        Bundle bundle = new Bundle();
        bundle.putString("mid", loginUser.getId()+"");
        intent(PersonInfoActivityTwo.class, bundle);
    }

    @OnClick(R.id.rl_my_dynamic)
    public void goMyDynamic() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        intent(MyDynamicActivity.class);
    }

    @OnClick({R.id.iv_head, R.id.tv_nickname, R.id.tv_id, R.id.iv_right, R.id.tv_edit})
    public void myUserInfo() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }

        User loginUser = SpConstant.getLoginUser();
        Bundle bundle = new Bundle();
        bundle.putString("mid", loginUser.getId()+"");
        intent(PersonInfoActivityTwo.class, bundle);
    }

    @OnClick(R.id.ll_my_collect)
    public void myCollect() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        Bundle bundle = new Bundle();
        intent(MyCollectBillActivity.class, bundle);
    }


    class MessageCountCall implements DataCall<MessageCount> {

        @Override
        public void success(MessageCount data, Object... args) {
            if (data.getBuyItCount() > 0) {
                tv_count_one.setVisibility(View.VISIBLE);
                tv_count_one.setText(data.getBuyItCount() + "");
            } else {
                tv_count_one.setVisibility(View.GONE);
            }
            if (data.getServiceCount() > 0) {
                tv_count_two.setVisibility(View.VISIBLE);
                tv_count_two.setText(data.getServiceCount() + "");
            } else {
                tv_count_two.setVisibility(View.GONE);
            }
//            if (data.getCommentCount() > 0) {
//                tv_count_three.setVisibility(View.VISIBLE);
//                tv_count_three.setText(data.getCommentCount() + "");
//            } else {
//                tv_count_three.setVisibility(View.GONE);
//            }
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class UserInfoCall extends SpUserInfoCall {

        @Override
        public void success(User data, Object... args) {
            super.success(data,args);
            setLogin(data.getServiceLevel(),data.isVip(),data.getVipTime());

            Glide.with(getActivity()).load(data.getImgUrl())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(iv_head);
            tv_shoucang.setText(data.getFavorites() + "");
            tv_visit_history.setText(data.getHistoryCount() + "");
            tv_see_me.setText(data.getVisitor() + "");
            tv_money.setText("￥" + data.getBalance());
            nickname.setText(data.getNickName());
            id.setText("id：" + data.getAutoId());

            spUtils.putLong(SpConstant.USER_MID, data.getId());
            spUtils.putBoolean(SpConstant.VIP, data.isVip());
            spUtils.putString(SpConstant.FIRST_NAME, data.getFirstName());
            spUtils.putString(SpConstant.NICK_NAME, data.getNickName());
            spUtils.putInt(SpConstant.AUTOID, data.getAutoId());
            if (!TextUtils.isEmpty(data.getPassword())) {
                spUtils.putString(SpConstant.PASSWORD, data.getPassword());
            }
            if (!TextUtils.isEmpty(data.getPayPwd())) {
                spUtils.putString(SpConstant.PAY_PWD, data.getPayPwd());
            }

        }

        @Override
        public void fail(ApiException e, Object... args) {
           setNoLogin();
        }
    }

}
